<!-- 检查模版 -->
<template>
	<view class="todo-page">
		<!-- 页面头部 -->
		<view class="page-header">
			<view class="header-content">
				<text class="page-title">检查模版</text>
				<!--<text class="task-count">{{ cpatrolList.length }} 个任务</text> -->
			</view>
		</view>

		<!-- 空状态 -->
		<view v-if="!isLoading && cpatrolList.length === 0" class="empty-state">
			<!--<text class="empty-icon">📄</text> -->
			<text class="empty-title">暂无检查模版</text>
			<!--<text class="empty-desc">您的待办巡查任务将显示在这里</text>-->
		</view>

		<!-- 任务列表 -->
		<view v-else class="task-list">
			<view 
				v-for="(item, index) in cpatrolList" 
				:key="item.pid || index" 
				class="task-card"
				@click="updateCpatrol(item)"
			>
				<!-- 卡片头部 -->
				<view class="card-header">
					<view class="task-time">
						<text class="time-icon">⏰</text>
						<text class="time-text">{{ formatTime(item.inserttime) }}</text>
					</view>
					<StatusBadge 
						:text="item.presult || '未知'"
						:type="getResultType(item.presult)"
					/>
				</view>

				<!-- 项目名称 -->
				<view class="project-name">
					<text class="name-text">{{ item.cproject || '未知项目' }}</text>
				</view>

				<!-- 任务信息 -->
				<view class="task-info">
					<view class="info-row">
						<text class="info-label">📝 单号</text>
						<text class="info-value">{{ item.pid || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">📋 类型</text>
						<text class="info-value">{{ item.ptype || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">🔍 模式</text>
						<text class="info-value">{{ item.pmode || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">👤 巡查人</text>
						<text class="info-value">{{ item.pman1 || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">⏰ 巡查时间</text>
						<text class="info-value">{{ formatDateTime(item.patroltime) }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">📊 流程节点</text>
						<text class="info-value flow-node">{{ getFlowNodeName(item.rf_flow_node) }}</text>
					</view>
					<view v-if="item.totalscore" class="info-row score-row">
						<text class="info-label">🏅 总得分</text>
						<text class="score-value">{{ item.totalscore }}分</text>
					</view>
				</view>

				<!-- 待办标记 -->
				<view class="pending-badge">
					<text class="pending-icon">⏳</text>
					<text class="pending-text">待办</text>
				</view>

				<!-- 操作按钮区 -->
				<view class="action-section">
					<text class="action-hint">点击查看详情或继续处理</text>
				</view>
			</view>
		</view>

		<!-- 加载状态 -->
		<LoadingSpinner v-if="isLoading" :overlay="true" text="加载中..." />
	</view>
</template>

<script>
	import { request } from '@/request/index.js'
	import StatusBadge from '@/components/common/StatusBadge.vue'
	import LoadingSpinner from '@/components/common/LoadingSpinner.vue'
	import { FLOW_NODES } from '@/constants/index.js'
	import { formatDate, formatDateTime } from '@/utils/index.js'
	
	export default {
		name: 'TodoPage',
		components: {
			StatusBadge,
			LoadingSpinner
		},
		data() {
			return {
				cpatrolList: [],
				userInfo: {},
				isLoading: false
			}
		},
		onLoad() {
			const userInfo = uni.getStorageSync('user')
			if (!userInfo || userInfo === 'null' || userInfo === '') {
				uni.showToast({
					title: '请先登录',
					success() {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
				})
				return
			}
			
			this.userInfo = userInfo
			this.getCpatrol()
		},
		onPullDownRefresh() {
			this.getCpatrol()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 800)
		},
		methods: {
			async getCpatrol() {
				try {
					this.isLoading = true
					
					const data = {
						sort: 'updatetime',
						order: 'desc',
						query_chman: this.userInfo.user.id
					}
					
					const response = await request({
						url: '/thirdGrid/query/c_patrol2-m_patrol2',
						method: 'POST',
						data: data,
						header: {
							'Content-Type': 'application/x-www-form-urlencoded',
							'cookie': uni.getStorageSync('cookie')
						}
					})
					
					if (response.data.success) {
						this.cpatrolList = response.data.rows || []
					} else {
						uni.showToast({
							title: response.data.msg || '加载失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('获取待办任务失败:', error)
					uni.showToast({
						title: '网络错误，请稍后重试',
						icon: 'none'
					})
				} finally {
					this.isLoading = false
				}
			},
			
			updateCpatrol(item) {
				if (!item || !item.pid) return
				
				uni.redirectTo({
					url: `/pages/updateCpatrol/updateCpatrol?pid=${item.pid}&ptype=${item.ptype_val || ''}`
				})
			},
			
			formatTime(timeStr) {
				if (!timeStr) return '--'
				return formatDate(timeStr)
			},
			
			formatDateTime(timeStr) {
				if (!timeStr) return '--'
				return formatDateTime(timeStr)
			},
			
			getResultType(result) {
				if (!result) return 'default'
				
				switch (result) {
					case '合格':
						return 'success'
					case '不合格':
						return 'error'
					default:
						return 'warning'
				}
			},
			
			getFlowNodeName(nodeCode) {
				if (!nodeCode) return '--'
				
				const node = Object.values(FLOW_NODES).find(n => n.code === nodeCode)
				return node ? node.name : nodeCode
			}
		}
	}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.todo-page {
	min-height: 100vh;
	padding: $spacing-lg;
	background: linear-gradient(135deg, #faad14 0%, #d48806 100%);
}

.page-header {
	margin-bottom: $spacing-lg;
	
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: $spacing-md 0;
	}
	
	.page-title {
		font-size: $font-size-xl;
		font-weight: 600;
		color: $text-white;
		text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.3);
	}
	
	.task-count {
		font-size: $font-size-sm;
		color: rgba(255, 255, 255, 0.9);
		background: rgba(255, 255, 255, 0.2);
		padding: $spacing-xs $spacing-sm;
		border-radius: $border-radius-lg;
	}
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx $spacing-xl;
	background: $white;
	border-radius: $border-radius-lg;
	box-shadow: $shadow-md;
	
	.empty-icon {
		font-size: 120rpx;
		margin-bottom: $spacing-lg;
		opacity: 0.6;
	}
	
	.empty-title {
		font-size: $font-size-lg;
		font-weight: 600;
		color: $text-primary;
		margin-bottom: $spacing-sm;
	}
	
	.empty-desc {
		font-size: $font-size-sm;
		color: $text-secondary;
		text-align: center;
		line-height: 1.5;
	}
}

.task-list {
	display: flex;
	flex-direction: column;
	gap: $spacing-lg;
}

.task-card {
	background: $white;
	border-radius: $border-radius-lg;
	padding: $spacing-xl;
	box-shadow: $shadow-md;
	position: relative;
	transition: all 0.3s ease;
	cursor: pointer;
	border-left: 4px solid $warning-color;
	
	&:active {
		transform: translateY(2rpx);
		box-shadow: $shadow-sm;
	}
}

.card-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: $spacing-lg;
	gap: $spacing-md;
}

.task-time {
	display: flex;
	align-items: center;
	gap: $spacing-xs;
	
	.time-icon {
		font-size: $font-size-sm;
		color: $text-secondary;
	}
	
	.time-text {
		font-size: $font-size-sm;
		color: $text-secondary;
	}
}

.project-name {
	margin-bottom: $spacing-lg;
	
	.name-text {
		font-size: $font-size-lg;
		font-weight: 600;
		color: $text-primary;
		line-height: 1.4;
	}
}

.task-info {
	display: flex;
	flex-direction: column;
	gap: $spacing-sm;
	margin-bottom: $spacing-lg;
}

.info-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: $spacing-sm;
	background: $gray-50;
	border-radius: $border-radius-base;
	
	.info-label {
		font-size: $font-size-sm;
		color: $text-secondary;
		font-weight: 500;
		min-width: 140rpx;
	}
	
	.info-value {
		font-size: $font-size-sm;
		color: $text-primary;
		font-weight: 500;
		
		&.flow-node {
			color: $primary-color;
			font-weight: 600;
		}
	}
	
	&.score-row {
		background: lighten($warning-color, 45%);
		border: 1px solid lighten($warning-color, 30%);
		
		.score-value {
			color: $warning-color;
			font-weight: 700;
			font-size: $font-size-base;
		}
	}
}

.pending-badge {
	position: absolute;
	top: $spacing-md;
	right: $spacing-md;
	display: flex;
	align-items: center;
	gap: $spacing-xs;
	background: $warning-color;
	color: $text-white;
	padding: $spacing-xs $spacing-sm;
	border-radius: $border-radius-lg;
	font-size: $font-size-xs;
	font-weight: 600;
	box-shadow: $shadow-sm;
	
	.pending-icon {
		font-size: $font-size-sm;
		font-weight: bold;
	}
	
	.pending-text {
		font-size: $font-size-xs;
	}
}

.action-section {
	text-align: center;
	padding-top: $spacing-md;
	border-top: 1px solid lighten($border-color, 10%);
	
	.action-hint {
		font-size: $font-size-xs;
		color: $text-muted;
		font-style: italic;
	}
}
</style>